home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Tools & Apps / OS⁄Toolbox / Apple Events / AE Word Services 1.0d6 / Writeswell Jr. Source / Scroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-23  |  5.2 KB  |  224 lines  |  [TEXT/KAHL]

  1. /* Scroll.c
  2.  * Handle window scrolling in Writeswell, Jr.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Apr 92 Mike Crawford
  14.  */
  15.  
  16. #include <EPPC.h>
  17. #include <AppleEvents.h>
  18. #include <AEObjects.h>
  19. #include "AERegistry.h"
  20. #include "TBConstants.h"
  21. #include "TBGlobals.h"
  22. #include "Scroll.h"
  23.  
  24.  
  25. void DoControl( WindowPtr theWindow, ControlHandle ctlHdl, short partCode, Point where )
  26. {
  27.     short    oldVal;
  28.     short    newVal;
  29.  
  30.     gScrollWindow = theWindow;
  31.  
  32.     switch ( partCode ){
  33.         case inUpButton:
  34.         case inDownButton:
  35.         case inPageUp:
  36.         case inPageDown:
  37.             
  38.             TrackControl( ctlHdl, where, TrackScrollBar );
  39.             break;
  40.         case inThumb:
  41.             oldVal = GetCtlValue( ctlHdl );
  42.             TrackControl( ctlHdl, where, (ProcPtr)NULL );
  43.             newVal = GetCtlValue( ctlHdl );
  44.             
  45.             if ( newVal != oldVal ){
  46.                 ScrollText( oldVal, newVal, ctlHdl );
  47.             }
  48.  
  49.             break;    
  50.     }
  51.  
  52.     return;
  53. }
  54.  
  55. pascal void TrackScrollBar( ControlHandle ctlHdl, short partCode )
  56. {
  57.     short        max;
  58.     short        min;
  59.     short        val;
  60.     short        newVal;
  61.     
  62.     max = GetCtlMax( ctlHdl );
  63.     min = GetCtlMin( ctlHdl );
  64.     val = GetCtlValue( ctlHdl );
  65.     newVal = val;
  66.  
  67.     switch ( partCode ){
  68.         case inUpButton:
  69.             if ( val > min )
  70.                 newVal = val - 1;
  71.             break;
  72.         case inDownButton:
  73.             if ( val < max )
  74.                 newVal = val + 1;
  75.             break;
  76.         case inPageUp:
  77.             newVal = val - gLinesPerPage;
  78.             
  79.             if ( newVal < min )
  80.                 newVal = min;
  81.             break;
  82.  
  83.         case inPageDown:
  84.             newVal = val + gLinesPerPage;
  85.             
  86.             if ( newVal > max )
  87.                 newVal = max;            
  88.             break;
  89.     }
  90.     
  91.     if ( newVal != val ){
  92.         ScrollText( val, newVal, ctlHdl );
  93.     }
  94.  
  95.     return;
  96. }
  97.  
  98. void ScrollText( short oldVal, short newVal, ControlHandle ctlHdl )
  99. {
  100.     TEHandle    textH;
  101.     short        dV;
  102.  
  103.     SetCtlValue( ctlHdl, newVal );
  104.     
  105.     textH = (TEHandle)GetWRefCon( gScrollWindow );
  106.     
  107.     dV = ( oldVal - newVal ) * (*textH)->lineHeight;
  108.     
  109.     TEScroll( 0, dV, textH );
  110.  
  111.     return;
  112. }
  113.  
  114. Boolean TrackContentClick( void )
  115. {
  116.     Point        where;
  117.     Rect        viewRect;
  118.     TEHandle    textH;
  119.     ControlHandle    ctlHdl;
  120.     GrafPtr            curPort;
  121.     
  122.     /* Track the mouse while it is clicked in the window, and scroll the textEdit
  123.      * field if necessary.  We must always return true.  We have to set the port
  124.      * so that GetMouse will have the right window's local coordinates.
  125.      */
  126.  
  127.     GetPort( &curPort );
  128.     SetPort( gScrollWindow );
  129.  
  130.     GetMouse( &where );
  131.  
  132.     textH = (TEHandle)GetWRefCon( gScrollWindow );
  133.     
  134.     viewRect = (*textH)->viewRect;
  135.     
  136.     ctlHdl = ((WindowPeek)gScrollWindow)->controlList;        /* We know there's 1 control */
  137.     
  138.     /* If the mouse is above or below the window, we fake a mouse click on the scroll bar */
  139.     if ( where.v < viewRect.top ){
  140.         TrackScrollBar( ctlHdl, inUpButton );
  141.     } else if ( where.v > viewRect.bottom ){
  142.         TrackScrollBar( ctlHdl, inDownButton );
  143.     } 
  144.  
  145.     SetPort( curPort );
  146.  
  147.     return true;
  148. }
  149.  
  150. void SizeVertScroll()
  151. {
  152.     MoveControl( gVertScroll, thePort->portRect.right - 15, -1 );
  153.     SizeControl( gVertScroll, 16, thePort->portRect.bottom - thePort->portRect.top - 13 );
  154.  
  155.     return;
  156. }
  157.  
  158. void SetVertScroll( WindowPtr theWindow, ControlHandle scrollHdl )
  159. {
  160.     short        totalHeight;
  161.     short        viewHeight;
  162.     TEHandle    hTE;
  163.     Rect        viewRect;
  164.     short        nLines;
  165.     short        tweenHeight;
  166.     short        i;
  167.  
  168.     /* The limits of the scroll bar range from 0 to the first line of the last
  169.      * "windowfull" of text.  That is, if there are 50 lines of text, and 10 lines
  170.      * would fit in the window, then the limits are 0 through 40.  If the window is
  171.      * resized so that 20 lines of text would fit, then the limits are 0 and 30.  This
  172.      * way, when the thumb is all the way at the bottom, the last line of text is at
  173.      * the bottom of the window.
  174.      */
  175.  
  176.     hTE = (TEHandle)GetWRefCon( theWindow );
  177.  
  178.     nLines = (*hTE)->nLines;
  179.  
  180.     totalHeight = TEGetHeight( 0, nLines - 1, hTE );
  181.     
  182.     viewRect = (*hTE)->viewRect;
  183.     viewHeight = viewRect.bottom - viewRect.top;
  184.  
  185.     /* STUB if the last line of text does not fall at or below the bottom of the window,
  186.      * scrol the text down so it does, but not so much that the top line of text is
  187.      * lower than the top of the window.
  188.      */
  189.  
  190.     if ( totalHeight < viewHeight ){
  191.     
  192.         /* All of the text will fit in the window.  Scroll the text so the first
  193.          * line is at the top, and turn off the scroll bar.
  194.          */
  195.         
  196.         /* STUB Scroll the text */
  197.         
  198.         HiliteControl( scrollHdl, 255 );
  199.     
  200.         return;
  201.     }
  202.     
  203.     /* The text does not all fit.  Find out how many lines from the bottom would fit */
  204.         
  205.     for ( i = nLines - 1; i > 0; i-- ){
  206.         tweenHeight = TEGetHeight( nLines, i, hTE );
  207.         
  208.         if ( tweenHeight >= viewHeight )
  209.             break;
  210.     }
  211.     
  212.     /* i is now the line number of the first line that would be in the window if
  213.      * we had scrolled all the way to the bottom
  214.      */
  215.     
  216.     SetCtlMax( scrollHdl, i );
  217.     HiliteControl( scrollHdl, 0 );
  218.  
  219.     /* Save the number of lines on a page for later use by scroll bar tracker */
  220.     
  221.     gLinesPerPage = nLines - i;
  222.  
  223.     return;
  224. }